package com.ymatou.iapi.coupon.testcase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponmanage.parameter.SellerCreateBatchBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CouponServiceMCallServiceV2;
import com.ymttest.database.model.couponbatch;
import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.model.couponbatch;
//import com.ymttest.database.sqlwapper.couponbatchWapper;
//import com.ymttest.database.sqlwapper.YmtCouponSettingWapper;
import com.ymatou.iapi.coupon.parameter.GetBatchInfosBean;
import com.ymatou.iapi.coupon.parameter.batchInfo;
import com.ymatou.iapi.coupon.parameter.batchInfos;
import com.ymatou.iapi.coupon.service.GetBatchInfosCall;

@TestSuite
public class Ts_GetBatchInfos {
	private static GetBatchInfosBean GetBatchInfosBean;
    private static GetBatchInfosCall GetBatchInfosCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("获取批次信息请求信息用例");
    }

    @Before
    public void caseUp() {
        GetBatchInfosBean = new GetBatchInfosBean();
        GetBatchInfosCall = new GetBatchInfosCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(GetBatchInfosCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_001() {
        Logger.start(true, "获取批次信息请求信息用例-单条");
        try {        	
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(list.get(0).get("BatchCode").toString());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_002() {
        Logger.start(true, "获取批次信息请求信息用例-多条");
        try {
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();

            for (Map batchCode : list) {
                batchCodes.add(batchCode.get("BatchCode").toString());
            }

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_003() {
        Logger.start(true, "获取批次信息请求信息用例-单条商家优惠券批次-正常状态");
        try {
        	CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();

            List<String> batchCodes = new ArrayList<String>();
          
			batchCodes.add(batchCode);

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            batchInfos batchInfos = GetBatchInfosCall.getData();
            batchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");

            Logger.verifyEquals(0, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_004() {
        Logger.start(true, "获取批次信息请求信息用例-单条-作废");
        try {
        	CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			
            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            // 作废优惠券
            couponCallService.InvalidSellerBatch(couponBatchBean.getSellerId(),batchCode);

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            batchInfos batchInfos = GetBatchInfosCall.getData();   
            batchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
            // 验证状态为作废：1
            Logger.verifyEquals(1, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

//    @Test
//    @Category({P1.class})
//    @TestCase
//    @Ignore
//    public void Tc_GetBatchInfos_005() {
//        Logger.start(true, "获取批次信息请求信息用例-单条-过期");
//        try {
//			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	
//
//			// 商家优惠券
//			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
//			String batchCode = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
//			
//            List<String> batchCodes = new ArrayList<String>();
//            batchCodes.add(batchCode);
//
//            // 设置优惠券过期
//            Date date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
//            int iCouponSettingId =
//                    new couponbatchWapper().selectBybatchCode(batchCode).get(0).getiCouponSettingId();
////            new YmtCouponSettingWapper().setiEffectiveType(iCouponSettingId, 0);
////            new YmtCouponSettingWapper().setdValidEnd(iCouponSettingId, date);
//
//
//            // 等五分钟刷新缓存
//            Thread.sleep(6*60000);
//            GetBatchInfosBean.setBatchCodes(batchCodes);
//
//            GetBatchInfosCall.setData(GetBatchInfosBean);
//            GetBatchInfosCall.callService();
//
//            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
//            Logger.verifyEquals("操作成功！",
//                    GetBatchInfosCall.getMessage(), "验证返回Message");
//
//            batchInfos batchInfos = GetBatchInfosCall.getData();
//            batchInfo batchInfo = batchInfos.getBatchInfos().get(0);
//
//            List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchCode);
//
//            Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
//                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
//            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
//            // 验证状态为过期：2
//            Logger.verifyEquals(2, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
//
//        } catch (Exception e) {
//            Logger.fail(e);
//        }
//    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_006() {
        Logger.start(true, "获取批次信息请求信息用例-单条-领完");
        try {
			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
			String batchCode = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			
            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add(batchCode);

            // 设置优惠券领完
            couponbatch batch = new couponbatchWapper().selectBybatchCode(batchCode).get(0);
            new couponbatchWapper().setiReceiveCount(batch.getBatchId(), batch.getTotalNum());
            
//            int iCouponSettingId = batch.getiCouponSettingId();
//            new YmtCouponSettingWapper().setiReceiveCount(iCouponSettingId, batch.getiCouponTotalNum());

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            batchInfos batchInfos = GetBatchInfosCall.getData();
            batchInfo batchInfo = batchInfos.getBatchInfos().get(0);

            List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchCode);

            Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
                    "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
            Logger.verifyEquals(batchCode, batchInfo.getBatchCode(), "验证返回BatchCode");
            // 验证状态为过期：3
            Logger.verifyEquals(3, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

//    @Test
//    @Category({P1.class})
//    @TestCase
//    @org.junit.Ignore
//    public void Tc_GetBatchInfos_007() {
//        Logger.start(true, "获取批次信息请求信息用例-多条多种状态组合");
//        try {
//			CouponServiceMCallServiceV2 couponCallService = new CouponServiceMCallServiceV2();	
//
//			// 商家优惠券
//			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();
//			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
//			String batchCode2 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
//			String batchCode3 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
//			String batchCode4 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
//			String batchCode5 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
//
//            List<String> batchCodes = new ArrayList<String>();
//            batchCodes.add(batchCode1);
//            batchCodes.add(batchCode2);
//            batchCodes.add(batchCode3);
//            batchCodes.add(batchCode4);
//            batchCodes.add(batchCode5);
//
//            // 作废优惠券
//            couponCallService.InvalidSellerBatch(couponBatchBean.getSellerId(),batchCode2);
//
//            // 设置优惠券过期
//            Date date = new Date(new Date().getTime() - 24 * 60 * 60 * 1000);
//            int iCouponSettingId3 =
//                    new couponbatchWapper().selectBybatchCode(batchCode3).get(0).getiCouponSettingId();
//            new YmtCouponSettingWapper().setiEffectiveType(iCouponSettingId3, 0);
//            new YmtCouponSettingWapper().setdValidEnd(iCouponSettingId3, date);
//
//            // 设置优惠券领完
//            couponbatch batch = new couponbatchWapper().selectBybatchCode(batchCode5).get(0);
//            int iCouponSettingId4 = batch.getiCouponSettingId();
//            new YmtCouponSettingWapper().setiReceiveCount(iCouponSettingId4, batch.getiCouponTotalNum());
//            
//            // 等五分钟刷新缓存
//            Thread.sleep(6*60000);
//            
//            GetBatchInfosBean.setBatchCodes(batchCodes);
//
//            GetBatchInfosCall.setData(GetBatchInfosBean);
//            GetBatchInfosCall.callService();
//
//            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
//            Logger.verifyEquals("操作成功！",
//                    GetBatchInfosCall.getMessage(), "验证返回Message");
//
//            batchInfos batchInfos = GetBatchInfosCall.getData();
//
//            for (batchInfo batchInfo : batchInfos.getBatchInfos()) {
//                List<couponbatch> results = new couponbatchWapper().selectBybatchCode(batchInfo.getBatchCode());
//
//                Logger.verifyEquals(results.get(0).getBatchId(), batchInfo.getBatchId(),
//                        "验证返回BatchId与数据库表Ymt_CouponBatch中一致");
//            }
//
//            // 验证状态
//            Logger.verifyEquals(0, batchInfos.getBatchInfos().get(0).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
//            Logger.verifyEquals(1, batchInfos.getBatchInfos().get(1).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
//            Logger.verifyEquals(2, batchInfos.getBatchInfos().get(2).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
//            Logger.verifyEquals(0, batchInfos.getBatchInfos().get(3).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
//            Logger.verifyEquals(3, batchInfos.getBatchInfos().get(4).getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
//
//        } catch (Exception e) {
//            Logger.fail(e);
//        }
//    }
    
    @Test
    @Category({P1.class})
    @TestCase
    public void Tc_GetBatchInfos_008() {
        Logger.start(true, "获取批次不存在");
        try {        	
            List<Map> list = new couponbatchWapper().selectTop5BatchInfoBysBatchCode();

            List<String> batchCodes = new ArrayList<String>();
            batchCodes.add("999999999");

            GetBatchInfosBean.setBatchCodes(batchCodes);

            GetBatchInfosCall.setData(GetBatchInfosBean);
            GetBatchInfosCall.callService();

            Logger.verifyEquals("0", GetBatchInfosCall.getStatusCode1(), "验证返回Code");
            Logger.verifyEquals("操作成功！",
                    GetBatchInfosCall.getMessage(), "验证返回Message");

            verifyBatchInfos();

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    private void verifyBatchInfos()
    {
        List<batchInfo> batchInfos = GetBatchInfosCall.getData().getBatchInfos();

        List<String> batchCodes = GetBatchInfosBean.getBatchCodes();

        int num = batchInfos.size();
        for (int i = 0; i < batchInfos.size(); i++) {
            //batchInfo batchInfo = batchInfos.get(num-i-1);
        	batchInfo batchInfo = batchInfos.get(i);
            couponbatch batch = new couponbatchWapper().selectBybatchCode(batchInfo.getBatchCode()).get(0);

            Logger.verifyEquals(batchCodes.get(i), batchInfo.getBatchCode(), "验证返回BatchCode为正常");
            Logger.verifyEquals(batch.getBatchId(), batchInfo.getBatchId(), "验证返回BatchI为正常");
            Logger.verifyEquals(batch.getCreaterType().intValue(), batchInfo.getCreaterType(), "验证返回CreaterType为正常");
            Logger.verifyEquals(batch.getReceiveTimesPerUser().intValue(), batchInfo.getMaxReceiveCountPerUser(), "验证返回maxReceiveCountPerUser为正常");

        }
    }

    private void verifyStatus(int BatchStatusInfo)
    {
        List<batchInfo> batchInfos = GetBatchInfosCall.getData().getBatchInfos();

        for (int i = 0; i < batchInfos.size(); i++) {
            batchInfo batchInfo = batchInfos.get(i);
            Logger.verifyEquals(BatchStatusInfo, batchInfo.getBatchStatusInfo(), "验证返回BatchStatusInfo为正常");
        }
    }
}
